<template>
  <div style="height: 65vh">
    <t-table
      rowKey="id"
      size="small"
      :data="data"
      :columns="site_tag === 'jtt'?columns:columns.filter(val=>{ return  val.title!=='计划类别'})"
      hover
      :pagination="pagination"
      :loading="dataLoading"
      :selected-row-keys="selectedRowKeys"
      :sort="sort"
      :hideSortTips="true"
      @sort-change="sortChange"
    >
      <template #[`contract.adviceFunding`]="{row}">
        {{row.contract.adviceFunding}}万元
      </template>
      <template #[`contract.subsidyFunding`]="{row}">
        {{row.contract.subsidyFunding}}万元
      </template>
      <template #changeType="{row}">
        {{ row.changeType | filterByDict(dict.type.change_type) }}
      </template>
      <template #[`project.subjectClassification`]="{ row }">
        {{ row.project.subjectClassification | filterByDict(dict.type.subject_classification) }}
      </template>
      <template #[`project.projectType`]="{ row }">
        {{ row.project.projectType | filterByDict(dict.type.project_type) }}
      </template>
      <template #[`project.projectCategory`]="{ row }">
        {{ row.project.projectCategory | filterParentsByTree(projectCategoryOptions,children = "children", id = "id", label = "name") }}
      </template>
      <template #[`project.projectStatus`]="{ row }">
        <t-tag @click="dialog.header=row.projectName;dialog.objId=row.id;dialog.open_approve_list=true" class="cursor-pointer"  :theme="handleTheme(row.project.projectStatus, dict.type.project_status)" variant="light">
          {{ row.project.projectStatus | filterByDict(dict.type.project_status) }}
        </t-tag>
      </template>
      <template #status="{ row }">
        <t-tag @click="dialog.header=row.contract.projectName;dialog.objId=row.projectId;dialog.open_approve_list=true" class="cursor-pointer" v-if="row.status" :theme="handleTheme(row.status,dict.type.approve_status)" variant="light">
          {{ row.status | filterByDict(dict.type.approve_status) }}
        </t-tag>
        <t-tag @click="dialog.header=row.contract.projectName;dialog.objId=row.projectId;dialog.open_approve_list=true" class="cursor-pointer" v-else theme="warning" variant="light">未提交</t-tag>
      </template>
      <template #op="{ row }">
        <t-button size="small" @click="update(row)" variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon"/>
          详情
        </t-button>
        <t-button @click="createPDF(row)" size="small"
                  variant="text" class="t-button-link">
          <t-icon name="file-pdf" slot="icon"/>
          导出PDF
        </t-button>
      </template>
    </t-table>
    <approve-list :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_approve_list" v-if="dialog.open_approve_list"></approve-list>
  </div>
</template>

<script>
import ApproveList from "../../approve/components/approve-list.vue";

export default {
  name: "change-apply-list",
  dicts: ['change_type', 'approve_status', 'project_status', 'subject_classification', 'project_type'],
  props: ["projectId"],
  components:{
    ApproveList
  },
  data(){
    return {
      site_tag: import.meta.env.VITE_SITE_TAG,
      selectedRowKeys:[],
      sort: [],
      projectCategoryOptions: [],
      dialog: {
        open_project_detail: false,
        header: "",
        objId: null,
        open: false,
        open_approve_list: false,
      },
      data: [],
      columns: [
        {
          colKey: "project.projectNum",
          title: "项目编号",
          align: "center",
          width: 120,
          // sorter: true,
          // ellipsis: true,
        },
        {
          colKey: "contract.projectName",
          title: "项目名称",
          align: "center",
          width: 220
        },
        {
          colKey: "changeType",
          title: "变更类型",
          align: "center",
          width: 100
        },
        {
          colKey: "contract.adviceFunding",
          title: "建议经费总概算",
          align: "center",
          width: 160,
          sorter: true,
        },
        {
          colKey: "contract.subsidyFunding",
          title: import.meta.env.VITE_SITE_TAG==='jtt'?"厅拨经费":`集团补助经费`,
          align: "center",
          sorter: true,
          width: 140,
        },
        {
          colKey: "contract.startDate",
          title: "任务书开始时间",
          align: "center",
          sorter: true,
          width: 160,
        },
        {
          colKey: "contract.completeDate",
          title: "任务书结束时间",
          align: "center",
          sorter: true,
          width: 160,
        },
        {
          colKey: "project.projectLeaderName",
          title: "项目负责人",
          align: "center",
          width: 120
        },
        {
          colKey: "nickName",
          title: "填报人",
          align: "center",
          width: 100
        },
        {
          colKey: "project.declareDeptName",
          title: "承担单位",
          align: "center",
          width: 180,
          sorter: true
        },
        {
          colKey: "project.projectCategory",
          title: "计划类别",
          align: "center",
          width: 120
        },
        {
          colKey: "project.subjectClassification",
          title: "学科分类",
          align: "center",
          width: 100
        },
        {
          colKey: "project.projectType",
          title: "项目类型",
          align: "center",
          width: 120
        },
        // {
        //   colKey: "createTime",
        //   title: "创建时间",
        //   align: "center",
        // },
        {
          colKey: "project.projectStatus",
          title: "项目状态",
          align: "center",
          width:220,
          fixed: 'right',
          sorter: true
        },
        {
          colKey: "status",
          title: "审批状态",
          align: "center",
          width: 120,
          fixed: 'right'
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 120,
          fixed: 'right'
        },
      ],
      dataLoading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: '',
        changeType: '',
        projectCategory: null,
        projectId: this.projectId,
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10,20,30],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  created(){
    this.getList();
    this.getDeclareTree();
  },
  methods:{
    getDeclareTree() {
      this.$api.kjxm.project.planCategoryList().then(response => {
        this.projectCategoryOptions = this.handleTree(response.data, "id");
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    sortChange(sort){
      this.sort = sort;
      let orderByColumn = []
      let isAsc = []
      if(sort instanceof Array){
        orderByColumn = sort.map(val=>val.sortBy)
        isAsc = sort.map(val=>val.descending?`desc`:`asc`)
      }else {
        orderByColumn = [sort.sortBy]
        isAsc = [sort.descending?`desc`:`asc`]
      }
      this.queryParams.orderByColumn = orderByColumn.join(",")
      this.queryParams.isAsc = isAsc.join(",")
      this.getList();
    },
    createPDF(row){
      const instance = this.$loading({
        fullscreen: true,
        attach: 'body',
        preventScrollThrough: false,
      });
      this.$api.kjxm.implement.changeApplyPDF(row.id).then(response => {
        window.open(response.data.fullFilePath, '_blank')
        instance.hide();
      }).catch((e) => {
        instance.hide();
        this.$message.error(e.toString());
      });
    },
    update(row){
      const taskId = row.id;
      sessionStorage.setItem(`${taskId}-changeApply-objId`, row.id);
      // 设置不可编辑
      sessionStorage.setItem(`${taskId}-changeApply-editOpen`, '0');
      this.$router.push({
        path: `/kjxm/other/implement/change-apply/${taskId}`,
        query: {
          title: `${row.contract.projectName}-变更申请`,
        }
      });
    },
    getList(){
      this.dataLoading = true;
      this.$api.kjxm.implement.changeApplyList(this.queryParams).then(response => {
        this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
  }
}
</script>

<style scoped>

</style>
